package com.lcc.lynx.common;

import lombok.Data;

import java.io.Serial;
import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 返回结果
 */
@Data
public class ResultVO<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    public static final int FAIL_CODE = 0;
    public static final int SUCCESS_CODE = 1;
    public static final int ERROR_CODE = 2;

    /**
     *  返回码 1：成功 0：失败
     */
    private int code;

    /**
     *  返回消息
     */
    private String message;

    /**
     *   返回数据
     */
    private T data;

    public ResultVO<Map<String, Object>> put(String key, Object object){
        if(!(this.getData() instanceof Map)){
            throw new IllegalArgumentException("result.data 必须为 Map 类型，推荐使用successPut()方法");
        }
        ResultVO<Map<String, Object>> result = (ResultVO<Map<String, Object>>) this;
        result.getData().put(key, object);
        return result;
    }

    public static ResultVO<Map<String, Object>> successPut(String key, Object object){
        ResultVO<Map<String, Object>> result = new ResultVO<>();
        result.setCode(SUCCESS_CODE);
        result.setMessage("成功");
        result.setData(new LinkedHashMap<>());
        result.getData().put(key, object);
        return result;
    }

    public static <T> ResultVO<T> success(){
        ResultVO<T> result = new ResultVO<T>();
        result.setCode(SUCCESS_CODE);
        result.setMessage("成功");
        return result;
    }

    public static <T> ResultVO<T> success(T data){
        ResultVO<T> result = new ResultVO<T>();
        result.setCode(SUCCESS_CODE);
        result.setMessage("成功");
        result.setData(data);
        return result;
    }

    public static <T> ResultVO<T> fail(String message){
        ResultVO<T> result = new ResultVO<T>();
        result.setCode(FAIL_CODE);
        result.setMessage(message);
        return result;
    }

    public static  <T> ResultVO<T> error(String message){
        ResultVO<T> result = new ResultVO<T>();
        result.setCode(ERROR_CODE);
        result.setMessage(message);
        return result;
    }

    public static  <T> ResultVO<T> ret(Integer code, String message){
        ResultVO<T> result = new ResultVO<T>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

}
